<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);

require_once $basedir . '/../../../../classes/minime/user/subscriptions.php';
require_once $basedir . '/../../../../classes/minime/user/subscription.php';

class Minime_User_SubscriptionsTest extends PHPUnit_Framework_TestCase {
    
    public function testConstructorOfMinimeUserSubscriptionsObject() {
        $object = new Minime_User_Subscriptions();
        $this->assertEquals("Minime_User_Subscriptions", get_class($object));
    }
    
    public function testBuildFromRawXml() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?>
<subscriptions>
  <subscription>
    <name>koski</name>
    <uri>http://wwww.gooogle.com/koski</uri>
    <atomfeed>http://identi.ca/api/statuses/user_timeline/209172.atom</atomfeed>
    <alternate>http://identi.ca/tkoski</alternate>
    <logo>http://www.logo-service.com/user.png</logo>
  </subscription>
  <subscription>
    <name>koski2</name>
    <uri>http://wwww.gooogle.com/koski2</uri>
    <atomfeed>http://identi.ca/api/statuses/user_timeline/209172.atom2</atomfeed>
    <alternate>http://identi.ca/tkoski2</alternate>
    <logo>http://www.logo-service.com/user2.png</logo>
  </subscription>
</subscriptions>
';
        $this->assertEquals($rawXML, Minime_User_Subscriptions::buildFromRawXML($rawXML)->asXML());
    }
    
    public function testAddSubscription() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?>
<subscriptions/>
';
        $subs = Minime_User_Subscriptions::buildFromRawXML($rawXML);
        $this->assertEquals($rawXML, $subs->asXML());
        
        $array['name'] = "koski";
        $array['uri'] = "http://wwww.gooogle.com/koski";
        $array['atomfeed'] = "http://www.alternate.com/atom.php?id=3216581321&id=852987465213";
        $array['alternate'] = "http://www.alternate.com/?id=3216581321&id=852987465213";
        $array['logo'] = "http://www.logo.com/user.asp?id=3216581321&id=852987465213";
        $subs->addSubscription(Minime_User_Subscription::buildFromArray($array));
        
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<subscriptions>
  <subscription>
    <name>koski</name>
    <uri>http://wwww.gooogle.com/koski</uri>
    <atomfeed>http://www.alternate.com/atom.php?id=3216581321&amp;id=852987465213</atomfeed>
    <alternate>http://www.alternate.com/?id=3216581321&amp;id=852987465213</alternate>
    <logo>http://www.logo.com/user.asp?id=3216581321&amp;id=852987465213</logo>
  </subscription>
</subscriptions>
';
        $this->assertEquals($expected, $subs->asXML());
    }
    
    public function testGetSubscriptionByUri() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?>
<subscriptions>
  <subscription>
    <name>koski</name>
    <uri>http://wwww.gooogle.com/koski</uri>
    <atomfeed>http://identi.ca/api/statuses/user_timeline/209172.atom</atomfeed>
    <alternate>http://identi.ca/tkoski</alternate>
    <logo>http://www.logo-service.com/user.png</logo>
  </subscription>
  <subscription>
    <name>koski2</name>
    <uri>http://wwww.gooogle.com/koski2</uri>
    <atomfeed>http://identi.ca/api/statuses/user_timeline/209172.atom2</atomfeed>
    <alternate>http://identi.ca/tkoski2</alternate>
    <logo>http://www.logo-service.com/user2.png</logo>
  </subscription>
</subscriptions>
';
        $subs = Minime_User_Subscriptions::buildFromRawXML($rawXML);
        $this->assertEquals("koski2", $subs->getSubscriptionByUri("http://wwww.gooogle.com/koski2")->getName());
    }
    
}